﻿'*******************************************************************************************
'* Copyright Brügmann Software GmbH, Papenburg
'* Author: Björn Kremer
'* Contact: codeplex<at>patorg.de
'* 
'* VB.CMIS is a VB.NET implementation of the Content Management Interoperability Services (CMIS) standard
'*
'* This file is part of VB.CMIS.
'* 
'* VB.CMIS is free software: you can redistribute it and/or modify
'* it under the terms of the GNU Lesser General Public License as published by
'* the Free Software Foundation, either version 3 of the License, or
'* (at your option) any later version.
'* 
'* VB.CMIS is distributed in the hope that it will be useful,
'* but WITHOUT ANY WARRANTY; without even the implied warranty of
'* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'* GNU Lesser General Public License for more details.
'* 
'* You should have received a copy of the GNU Lesser General Public License
'* along with VB.CMIS. If not, see <http://www.gnu.org/licenses/>.
'*******************************************************************************************
Public Class Common
   Private Shared _logFile As String = Nothing
   Private Shared _parseData As Boolean = True
   Private Shared _debug As Boolean = True
   Private Shared _doCaching As Boolean = True
   Private Shared _enableLogin As Boolean = True

   Shared Sub New()
      'Path of the logfile. 
      Dim logFile As String = System.Configuration.ConfigurationManager.AppSettings("LogFile")
      If Not String.IsNullOrEmpty(logFile) AndAlso IO.Directory.Exists(logFile) Then
         _logFile = logFile
      End If

      'Should the data from the client be parsed?
      Dim parseDataStr As String = System.Configuration.ConfigurationManager.AppSettings("ParseData")
      If Not String.IsNullOrEmpty(parseDataStr) Then
         If Not Boolean.Parse(parseDataStr) Then
            _parseData = False
         End If
      End If

      'Should the service return debug informations?
      Dim debugStr As String = System.Configuration.ConfigurationManager.AppSettings("Debug")
      If Not String.IsNullOrEmpty(debugStr) Then
         If Not Boolean.Parse(debugStr) Then
            _debug = False
         End If
      End If

      'Should the service cache the RepositoryInfo?
      Dim doCachingStr As String = System.Configuration.ConfigurationManager.AppSettings("DoCaching")
      If Not String.IsNullOrEmpty(doCachingStr) Then
         If Not Boolean.Parse(doCachingStr) Then
            _doCaching = False
         End If
      End If

      'Should the Login into the repository?
      Dim enableLoginStr As String = System.Configuration.ConfigurationManager.AppSettings("EnableLogin")
      If Not String.IsNullOrEmpty(enableLoginStr) Then
         If Not Boolean.Parse(enableLoginStr) Then
            _enableLogin = False
         End If
      End If
   End Sub
   ''' <summary>
   ''' Should the data from the client be parsed?
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Shared ReadOnly Property ParseData As Boolean
      Get
         Return _parseData
      End Get
   End Property
   ''' <summary>
   ''' Should the service return debug informations?
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Shared ReadOnly Property Debug As Boolean
      Get
         Return _debug
      End Get
   End Property
   ''' <summary>
   ''' Should the service cache the RepositoryInfo?
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Shared ReadOnly Property DoCaching As Boolean
      Get
         Return _doCaching
      End Get
   End Property

   ''' <summary>
   ''' Should the Login into the repository?
   ''' </summary>
   ''' <value></value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Shared ReadOnly Property EnableLogin As Boolean
      Get
         Return _enableLogin
      End Get
   End Property
   ''' <summary>
   ''' Converts as string to a nullable int.
   ''' </summary>
   ''' <param name="str"></param>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Shared Function ParseInt(str As String) As Integer?
      Dim retVal As Integer? = Nothing
      If String.IsNullOrEmpty(str) Then
         Return retVal
      End If
      Dim tmpInt As Integer
      If Integer.TryParse(str, tmpInt) Then
         retVal = tmpInt
      End If
      Return retVal
   End Function
   ''' <summary>
   '''  Converts as string to a nullable boolean.
   ''' </summary>
   ''' <param name="str"></param>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Shared Function ParseBoolean(str As String) As Boolean?
      Dim retVal As Boolean? = Nothing
      If String.IsNullOrEmpty(str) Then
         Return retVal
      End If
      Dim tmpInt As Boolean
      If Boolean.TryParse(str, tmpInt) Then
         retVal = tmpInt
      End If
      Return retVal
   End Function
   ''' <summary>
   '''  Converts as string to a nullable enum.
   ''' </summary>
   ''' <typeparam name="T"></typeparam>
   ''' <param name="str"></param>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Shared Function ParseEnum(Of T As {Structure})(str As String) As T?
      Dim retVal As T? = Nothing
      If String.IsNullOrEmpty(str) Then
         Return retVal
      End If
      Dim tmpEnum As T
      If [Enum].TryParse(str, tmpEnum) Then
         retVal = tmpEnum
      End If
      Return retVal
   End Function
   ''' <summary>
   ''' Adds a message to the log file.
   ''' </summary>
   ''' <param name="msg"></param>
   ''' <remarks></remarks>
   Public Shared Sub Log(msg As String)
      If Not String.IsNullOrEmpty(_logFile) Then
         IO.File.AppendAllText(_logFile, Now.ToString & " " & msg & vbNewLine)
      End If
   End Sub
   ''' <summary>
   ''' Adds a message to the log file.
   ''' </summary>
   ''' <param name="ex"></param>
   ''' <remarks></remarks>
   Public Shared Sub Log(ex As Exception)
      Log(ex.Message & " " & ex.StackTrace)
   End Sub
End Class
